import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import EmpView from '../views/EmpView.vue'
import DeptView from '../views/DeptView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    name: 'home',
    redirect: '/emp',
    component: Home,
    children: [
      {path: '/emp', name: 'emp', component: EmpView},
      {path: '/dept', name: 'dept', component: DeptView},
    ]

  },
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if(to.name != 'login' && sessionStorage.getItem("uname") == null) {
    next({name: 'login'})
  } else {
    next()//放行
  }
})

export default router
